package server.datamodel;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;


public class Cell {

	private String ID;
	private String type;
	private Map map;
	private int x;
	private int y;
	private float worldX;
	private float worldY;
	private Tower tower;
	private float value;
	private boolean hasTowerForSimulation;
	private boolean slowed;
	private Cell nearestSlowtowerCell;
	private HashMap<String, Waypoint> waypoints = new HashMap<String, Waypoint>();
	private ConcurrentHashMap<String, Creep> creeps = new ConcurrentHashMap<String, Creep>();
	
	public Cell() {
		this.ID = Datamodel.generateID(this);
	}
	
	@Override
	public String toString() {
		return ID;
	}
	
	public Waypoint getWaypoint(String ID) {
		return waypoints.get(ID);
	}
	
	public void addWaypoint(Waypoint waypoint) {
		if(!waypoints.containsKey(waypoint.getID())) {
			waypoints.put(waypoint.getID(), waypoint);
			waypoint.setCell(this);
		}
	}
	
	public void removeWaypoint(Waypoint waypoint) {
		if(waypoints.remove(waypoint.getID()) != null) {
			waypoint.setCell(null);
		}
	}
	
	public void setMap(Map map) {
		if(this.map != map) {
			if(getMap() != null) {
				Map oldMap = getMap();
				this.map = null;
				oldMap.removeCell(this);
			}
			this.map= map;
			if(getMap() != null) {
				getMap().addCell(this);
			}
		}
	}
	
	public Map getMap() {
		return map;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getID() {
		return ID;
	}

	public void setID(String ID) {
		this.ID = ID;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	public Tower getTower() {
		return tower;
	}
	
	public void setTower(Tower tower) {
		if(this.tower != tower) {
			if(getTower() != null) {
				Tower oldTower = getTower();
				this.tower = null;
				oldTower.setCell(null);
			}
			this.tower = tower;
			if(getTower() != null) {
				getTower().setCell(this);
			}
		}
	}
	
	public Creep getCreep(String ID) {
		return creeps.get(ID);
	}
	
	public void addCreep(Creep creep) {
		if(!creeps.containsKey(creep.getID())) {
			creeps.put(creep.getID(), creep);
			creep.setCell(this);
		}
	}
	
	public void removeCreep(Creep creep) {
		if(creeps.remove(creep.getID()) != null) {
			creep.setCell(null);
		}
	}
	
	public ConcurrentHashMap<String, Creep> getCreeps() {
		return creeps;
	}
	
	public float getWorldX() {
		return worldX;
	}


	public float getWorldY() {
		return worldY;
	}

	public void setWorldX(float worldX) {
		this.worldX = worldX;
	}

	public void setWorldY(float worldY) {
		this.worldY = worldY;
	}

	public float getValue() {
		return value;
	}

	public void setValue(float value) {
		this.value = value;
	}

	public boolean isHasTowerForSimulation() {
		return hasTowerForSimulation;
	}

	public void setHasTowerForSimulation(boolean hasTowerForSimulation) {
		this.hasTowerForSimulation = hasTowerForSimulation;
	}

	public void setSlowed(boolean slowed) {
		this.slowed = slowed;
	}

	public boolean getSlowed() {
		return slowed;
	}

	public void setNearestSlowtowerCell(Cell nearestSlowtowerCell) {
		this.nearestSlowtowerCell = nearestSlowtowerCell;
	}

	public Cell getNearestSlowtowerCell() {
		return nearestSlowtowerCell;
	}


}
